package com.chuanc.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;

import com.astuetz.PagerSlidingTabStrip;
import com.chuanc.R;
import com.chuanc.util.ThemeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Angel on 2017/10/17.
 */

public class TabNetPagerFragment extends AttachFragment {

    private ViewPager viewpager;
    private int page = 0;
    private boolean isFirstLoad = true;

    public static final TabNetPagerFragment newInstance(int page, String[] title) {
        TabNetPagerFragment f = new TabNetPagerFragment();
        Bundle bdl = new Bundle(1);
        bdl.putInt("page_number", page);
        f.setArguments(bdl);
        return f;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewpager.setCurrentItem(page);
    }

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_net_tab,container,false);
        viewpager= (ViewPager) view.findViewById(R.id.viewpager);
        if (viewpager != null) {
            setupViewPager(viewpager);
            viewpager.setOffscreenPageLimit(2);
        }
        final TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
//        tabLayout.setTabTextColors(R.color.text_color, ThemeUtils.getThemeColorStateList(mContext, R.color.theme_color_primary).getDefaultColor());
//        tabLayout.setSelectedTabIndicatorColor(ThemeUtils.getThemeColorStateList(mContext, R.color.theme_color_primary).getDefaultColor());
        tabLayout.setupWithViewPager(viewpager);
        return view;
    }
    MusicFragment musicFragment;
    private void setupViewPager(ViewPager viewPager) {
        Adapter adapter = new Adapter(getChildFragmentManager());
        musicFragment=new MusicFragment();
        adapter.addFragment(musicFragment, "音乐");
        adapter.addFragment(new VideoFragment(), "视频");
        adapter.addFragment(new RadioFragment(), "电台");
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
    }

//    @Override
//    public void setUserVisibleHint(boolean isVisibleToUser) {
//        super.setUserVisibleHint(isVisibleToUser);
//        if(musicFragment == null){
//            return;
//        }
//        if(isVisibleToUser && isFirstLoad){
//            isFirstLoad = false;
//        }
//    }

//    public void changeTo(int page) {
//        if (viewpager != null)
//            viewpager.setCurrentItem(page);
//    }

    static class Adapter extends FragmentStatePagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }
    }

}
